﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.Pocos;
using Timeclock.Contexts;

namespace Timeclock.Controllers
{ 
    public class OfficeController : Controller
    {
        private TimeclockContext db = new TimeclockContext();

        //
        // GET: /Office/

        public ViewResult Index()
        {
            return View(db.Offices.ToList());
        }

        //
        // GET: /Office/Details/5

        public ViewResult Details(int id)
        {
            Office office = db.Offices.Find(id);
            return View(office);
        }

        //
        // GET: /Office/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Office/Create

        [HttpPost]
        public ActionResult Create(Office office)
        {
            if (ModelState.IsValid)
            {
                office.createDate = DateTime.Now;
                office.updateDate = DateTime.Now;

                db.Offices.Add(office);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(office);
        }
        
        //
        // GET: /Office/Edit/5
 
        public ActionResult Edit(int id)
        {
            Office office = db.Offices.Find(id);
            return View(office);
        }

        //
        // POST: /Office/Edit/5

        [HttpPost]
        public ActionResult Edit(Office office)
        {
            if (ModelState.IsValid)
            {
                //var db2 = new TimeclockContext();
                //var o = db2.Offices.Find(office.officeId);

                //office.createDate = o.createDate;
                
                //db2.Dispose();

                //db.Offices.Attach(office);
                //db.Entry(office).Property(u => u.createDate).IsModified = false;

                //db.Entry(office).State = EntityState.Modified;

                db.Offices.Attach(office);
                office.updateDate = DateTime.Now;

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(office);
        }

        //
        // GET: /Office/Delete/5
 
        public ActionResult Delete(int id)
        {
            Office office = db.Offices.Find(id);
            return View(office);
        }

        //
        // POST: /Office/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Office office = db.Offices.Find(id);
            db.Offices.Remove(office);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}